package cn.b.sky.shop.activiti.service.impl;

import cn.b.sky.shop.activiti.constans.GlobalDefine;
import cn.b.sky.shop.activiti.dao.WaitTaskDao;
import cn.b.sky.shop.activiti.model.WaitTask;
import cn.b.sky.shop.activiti.model.condition.WaitTaskCondition;
import cn.b.sky.shop.activiti.service.WaitTaskService;
import cn.b.sky.common.model.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("waitTaskService")
public class WaitTaskServiceImpl implements WaitTaskService {
  @Autowired
  private WaitTaskDao waitTaskDao;

  @Override
  public void saveWaitTask(WaitTask waitTask) {
    if (waitTask != null) {
      waitTaskDao.insertWaitTask(waitTask);
    }

  }

  @Override
  public Page selectWaitTaskByCondition(WaitTaskCondition condition) {
    List<WaitTask> waitTasks = waitTaskDao.selectWaitTaskByCondition(condition);
    int count = waitTaskDao.selectWaitTaskCountByCondition(condition);
    condition.setTotalCount(count);
    condition.setData(waitTasks);
    return condition;
  }

  @Override
  public WaitTask getStartWaitTaskByProcessInstance(String tenantId, String processInstanceId) {

    if (StringUtils.isBlank(processInstanceId)) {
      return null;
    }
    return waitTaskDao.selectStarWaitTask(tenantId, processInstanceId, GlobalDefine.WorkFlow.STARTPROCESS);
  }

}
